package com.stenio.fancat.service;

import java.util.List;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.stenio.fancat.common.FanCatException;
import com.stenio.fancat.domain.RechOrder;
import com.stenio.fancat.domain.RechPaytype;
import com.stenio.fancat.domain.RechProduct;
import com.stenio.fancat.domain.bo.RechIndexBO;
import com.stenio.fancat.form.OrderForm;
import com.stenio.fancat.form.RechOrderForm;
import com.stenio.fancat.form.UserForm;
import com.stenio.fancat.mapper.RechOrderMapper;
import com.stenio.fancat.mapper.RechPaytypeMapper;
import com.stenio.fancat.mapper.RechProductMapper;
import com.stenio.fancat.pay.OrderResult;
import com.stenio.fancat.pay.Pay;
import com.stenio.fancat.util.SnowflakeIdWorker;

/**
 * 充值candy订单模块服务实现类
 */
@Service
public class RechOrderService extends ServiceImpl<RechOrderMapper, RechOrder> {

	@Autowired
	private Pay pay;
	@Autowired
	private RechOrderMapper rechOrderMapper;
	@Autowired
	private RechPaytypeMapper rechPaytypeMapper;
	@Autowired
	private RechProductMapper rechProductMapper;
	@Autowired
	private UserService userService;

	/**
	 * 通过用户Id查询充值candy订单列表
	 * 
	 * @param userId
	 * 
	 * @return
	 */
	public Page<RechOrder> queryOrderByUserId(RechOrderForm rechOrderForm) {
		if (null == rechOrderForm || null == rechOrderForm.getUserId()) {
			throw new RuntimeException("用户信息不能为空");
		}
		Page<RechOrder> page = new Page<RechOrder>(rechOrderForm.getCurrentPageNo(), rechOrderForm.getPageSize());
		return selectPage(page, new EntityWrapper<RechOrder>().eq(RechOrder.USER_ID, rechOrderForm.getUserId()));
	}

	/**
	 * 保存充值candy订单，并调用微信支付
	 * 
	 * @param rechOrder
	 * @throws FanCatException
	 */
	@SuppressWarnings("static-access")
	public OrderResult saveRechOrder(RechOrderForm rechOrderForm) throws FanCatException {
		if (null == rechOrderForm || null == rechOrderForm.getAmount() || null == rechOrderForm.getRechPaytypeId()
				|| null == rechOrderForm.getRechProdId() || null == rechOrderForm.getUserId()) {
			throw new RuntimeException("参数不能为空");
		}
		// 获取支付类型信息
		RechPaytype rechPaytype = rechPaytypeMapper.selectById(rechOrderForm.getRechPaytypeId());
		if (null == rechPaytype || StringUtils.isBlank(rechPaytype.getName()))
			throw new RuntimeException("支付类型信息错误");
		// 获取虚拟商品信息
		RechProduct rechProduct = rechProductMapper.selectById(rechOrderForm.getRechProdId());
		if (null == rechProduct || StringUtils.isBlank(rechPaytype.getName()))
			throw new RuntimeException("商品信息错误或下架");
		RechOrder rechOrder = new RechOrder();
		try {
			BeanUtils.copyProperties(rechOrder, rechOrderForm);
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException("参数转换有误");
		}
		rechOrder.setCandy(rechProduct.getCandy());
		rechOrder.setPaytypeName(rechPaytype.getName());
		rechOrder.setStatus(1);
		rechOrder.setRechOrderNO(new String().valueOf(new SnowflakeIdWorker(0, 0).nextId()));
		rechOrderMapper.insertSelective(rechOrder);
		OrderForm orderForm = new OrderForm();
		// 支持128个字符
		orderForm.setBody(rechOrder.getRechOrderNO());
		orderForm.setRechOrderNO(rechOrder.getRechOrderNO());
		orderForm.setFee(rechOrder.getAmount() * 100);// 按分提交到微信
		orderForm.setIp(rechOrder.getIpAddr());
		OrderResult result;
		try {
			result = pay.placeOrder(orderForm);
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException("微信支付发起异常");
		}
		return result;
	}

	/**
	 * 获取充值首页初始化数据，包含虚拟商品、支付方式、用户账户信息等
	 * 
	 * @param rechOrder
	 */
	public RechIndexBO queryRechIndex(RechOrderForm rechOrderForm) {
		if (null == rechOrderForm || null == rechOrderForm.getUserId()) {
			throw new RuntimeException("参数不能为空");
		}
		RechIndexBO riBO = new RechIndexBO();
		// 获取虚拟商品列表
		List<RechProduct> rechProductList = rechProductMapper.selectList(new EntityWrapper<RechProduct>());
		riBO.setProducts(rechProductList);
		// 获取支付方式列表
		List<RechPaytype> rechPaytypeList = rechPaytypeMapper.selectList(new EntityWrapper<RechPaytype>());
		riBO.setPayments(rechPaytypeList);
		// 获取用户账户信息
		UserForm userForm = new UserForm();
		userForm.setUserId(rechOrderForm.getUserId());
		riBO.setUserInfo(userService.queryUserInfoByCondition(userForm));
		return riBO;
	}
}
